﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using FarmerJoe.ClassSettings;
using FarmerJoe.Settings;
using Styx;

namespace FarmerJoe
{
    public partial class ucDruidSetting : UserControl
    {
        private bool _isLoading = true;

        public ucDruidSetting()
        {
            InitializeComponent();
            PopulateUIControls();

            switch (StyxWoW.Me.Specialization)
            {
                case WoWSpec.DruidRestoration:
                    tabControl1.SelectTab("Restoration");
                    break;

                case WoWSpec.DruidFeral:
                    tabControl1.SelectTab("FeralCat");
                    break;

                case WoWSpec.DruidGuardian:
                    tabControl1.SelectTab("Guardian");
                    break;

                case WoWSpec.DruidBalance:
                    tabControl1.SelectTab("Balance");
                    break;
            }

            _isLoading = false;
        }

        private void PopulateUIControls()
        {
            SuspendLayout();

            Type type = typeof(DruidSettings); PropertyInfo[] settingsProperties = type.GetProperties();
            foreach (PropertyInfo setting in settingsProperties)
            {
                PropertyInfo pInfo = type.GetProperty(setting.Name);
                object propValue = pInfo.GetValue(FpswareSettings.Instance.Druid, null);

                foreach (GroupBox gb in from TabPage tab in tabControl1.Controls from GroupBox gb in tab.Controls select gb)
                    foreach (object obj in gb.Controls)
                    {
                        // Populate all combo boxes, this will be the most common
                        if (obj is ucDropdown)
                        {
                            var cbox = (ucDropdown) obj;

                            cbox.ValueChanged += ucControl_ValueChanged;
                            if (cbox.SettingName == setting.Name && cbox.Value != propValue.ToString())
                            {
                                cbox.Value = propValue.ToString();
                                break;
                            }
                        }

                            // Populate all progress bars, mostly for health or mana 
                        else if (obj is ucProgressBar)
                        {
                            var pbar = (ucProgressBar) obj;

                            pbar.ValueChanged += ucControl_ValueChanged;

                            if (pbar.SettingName == pInfo.Name)
                            {
                                pbar.Value = (int) propValue;
                                break;
                            }
                        }
                    }
            }

            _isLoading = false;
            ResumeLayout();
        }

        private void ucDruidSetting_Load(object sender, System.EventArgs e)
        {

        }

        private void ucControl_ValueChanged(object sender, EventArgs e)
        {
            // Basically enumerate the [Druid] setting class and find the Property that matches the name of 'e.SettingName' and assign the value of 'e.Value'
            // This saves us creating code for every item / setting and adds reusability

            if (_isLoading) return;

            Type type = typeof(DruidSettings); PropertyInfo[] settingsProperties = type.GetProperties();
            foreach (PropertyInfo setting in settingsProperties.Where(setting => e.SettingName == setting.Name))
            {
                setting.SetValue(FpswareSettings.Instance.Druid, e.Value, null);
            }
        }

        private void ucControl_ValueChanged(object sender, cbEventArgs e)
        {
            // Basically enumerate the [Druid] setting class and find the Property that matches the name of 'e.SettingName' and assign the value of 'e.Value'
            // This saves us creating code for every item / setting and adds reusability

            if (_isLoading) return;

            Type type = typeof(DruidSettings); PropertyInfo[] settingsProperties = type.GetProperties();
            foreach (PropertyInfo setting in settingsProperties.Where(setting => e.SettingName == setting.Name))
            {
                setting.SetValue(FpswareSettings.Instance.Druid, e.Value, null);
            }
        }

        private void Common_Click(object sender, System.EventArgs e)
        {
            
        }

        private void ucDropdown4_Load(object sender, System.EventArgs e)
        {

        }



        private void ucTigersFury_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Min Energy","TigersFuryMinEnergy",0,100,"Druid","The minimum energy at which this will be cast");

            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example; cast Tiger's Fury but only when you energy is below 20 [Min Energy].";
            frm2.FormHeading = "Tiger's Fury";
            frm2.Show(this);
            
        }

        private void ucFeralIncarnation_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Pounce", "IncarnationPounce", new List<string>(){"... always", "... never"}, "Druid", "If enabled, during Incarnation cast Pounce to stun targets");
            frm2.AddDropDown("Ravage", "IncarnationRavage", new List<string>() { "... always", "... never" }, "Druid", "If enabled, during Incarnation cast Ravage");

            frm2.FormHeading = "Incarnation";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example; if you have Incarnation buff cast Pounce and / or Ravage. If you don't want to cast Pounce simply set it to 'never'.";
            frm2.Show(this);
        }

        private void ucFeralFinisher_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Target Health", "FinisherTargetHealth",0,100, "Druid", "The maximum health of the target before the finisher is considered. Recommended 25-35");
            frm2.AddDropDown("Min Combo Points", "FinisherComboPoints", new List<string>() { "1","2","3","4","5" }, "Druid", "The minimum combo points before the finisher is considered. Recommended 1-2");

            frm2.FormHeading = "Finisher: " + FpswareSettings.Instance.Druid.Finisher ;
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example; Cast " + FpswareSettings.Instance.Druid.Finisher + " when the target's health is no higher than 30% [Target Health] and you have at least 1 combo point [Min Combo Points]";

            frm2.Show(this);
        }

        private void ucFeralFinisher_Load(object sender, System.EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, System.EventArgs e)
        {

        }

        private void ucSavageDefense_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Health", "SavageDefenseHealth", 0, 100, "Druid", "The minimum health percent what which Savage Defense will be cast");

            frm2.FormHeading = "Savage Defense";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example; Cast Savage Defense when your health is lower than 70 % [Health]. If your health is above this value it will not be cast.";

            frm2.Show(this);
        }

        private void ucSavageDefense_Load(object sender, System.EventArgs e)
        {

        }

        private void ucSavageRoar_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Max Combo Points", "SavageRoarMaxCP", new List<string>() { "1", "2", "3", "4", "5" }, "Druid", "The maximum combo points to cast Savage Roar.  Recommended 2");

            frm2.FormHeading = "Savage Roar";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example; Only cast Savage Roar when your combo points is <= 2 [Max Combo Points]. If your combo points are above this it will not be cast";

            frm2.Show(this);
        }

        private void ucDisorientingRoar_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {

        }

        private void ucFeralPull_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Faerie Fire", "FeralPullFaerieFire", new List<string>() { "... always","... never" }, "Druid", "Flying targets will ALWAYS be pulled with Faerie Fire regardless of stealth");
            frm2.AddDropDown("Prowl", "FeralPullProwl", new List<string>() { "... always", "... never" }, "Druid", "When prowling, all appropriate stealth spells will be used");
            frm2.AddDropDown("Pounce", "FeralPullPounce", new List<string>() { "... always", "... never" }, "Prowl Pulling", "Use Pounce as an opening when prowling");
            frm2.AddDropDown("Ravage", "FeralPullRavage", new List<string>() { "... always", "... never" }, "Prowl Pulling", "Use Ravage as an opening when prowling");

            frm2.FormHeading = "Feral Pull";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "Using Prowl to pull will override Faerie Fire. If both settings are set to never a basic melee pull will be used.";

            frm2.Show(this);
        }

        private void ucBalancePull_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Starsurge", "BalancePullStarsurge", new List<string>() { "... always", "... never" }, "", "");
            frm2.AddDropDown("Entangling Roots", "BalancePullEntanglingRoots", new List<string>() { "... always", "... never" }, "", "");
            frm2.AddDropDown("Starfire", "BalancePullStarfire", new List<string>() { "... always", "... never" }, "", "");
            frm2.AddDropDown("Moonfire", "BalancePullMoonfire", new List<string>() { "... always", "... never" }, "", "");
            frm2.AddDropDown("Wrath", "BalancePullWrath", new List<string>() { "... always", "... never" }, "", "");

            frm2.FormHeading = "Balance Pull";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "Pull spells will be checked in the order they are displayed here. Eg Starsurge will be checked before Starfire.";

            frm2.Show(this);
        }

        private void ucBalancePull_Load(object sender, System.EventArgs e)
        {

        }

        private void ucWildMushrooms_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Minimum of Mobs", "WildMushroomMinMobs", new List<string>() {"1", "2", "3", "4", "5", "6", "7" }, "Wild Mushroom", "The minimum number of mobs within 10 yards of eachother before mushrooms are placed");
            frm2.AddDropDown("Mushroom Detonate", "WildMushroomMinDetonate", new List<string>() { "1", "2", "3"}, "Wild Mushroom", "The minimum number of mushrooms before detonate is cast");

            frm2.FormHeading = "Wild Mushrooms";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "Eg When there are 5 [Minimum of Mobs] mobs in 10 yards of each other drop mushrooms. When you've dropped 2 [Mushroom Detonate] cast detonate.";

            frm2.Show(this);
        }

        private void ucTranquility_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Tranquility Health", "TranquilityHealth", 0,100, "Tranquility", "The minimum health percent of players before tranquility is considered");
            frm2.AddDropDown("Min Count", "TranquilityCount", new List<string>() {"2", "3","4","5" }, "Tranquility", "The minimum number of players who need to be below [tranquility health].");

            frm2.FormHeading = "Tranquility";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: Cast Tranquility when 3 [Min Count] players are below 60% [Tranquility Health] health";

            frm2.Show(this);
        }

        private void ucWildGrowth_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Wild Growth Health", "WildGrowthHealth", 0, 100, "Wild Growth", "The minimum health percent of players before Wild Growth is considered");
            frm2.AddDropDown("Min Count", "WildGrowthCount", new List<string>() { "2", "3", "4", "5" }, "Wild Growth", "The minimum number of players who need to be below [wild growth health].");

            frm2.FormHeading = "Wild Growth";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: Cast Wild Growth when 3 [Min Count] players are below 60% [Wild Growth Health] health";

            frm2.Show(this);
        }

        private void ucMightOfUrsoc_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Health", "MightOfUrsocBearHealth", 0, 100, "Might of Ursoc", "The minimum health percent before Might of Ursoc is considered");
            frm2.AddDropDown("When", "MightOfUrsocBearWhen", new List<string>() { "... always", "... only on adds", "... only on 3+ adds", "... only on 4+ adds", "... only on 5+ adds" }, "", "");

            frm2.FormHeading = "Might of Ursoc";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: Cast Might of Ursoc when your health is below 60% [Health] and your have 3+ adds [When].";

            frm2.Show(this);
        }

        private void ucRip_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Min Target Health", "RipMinTargetHealth", 0, 100, "Rip", "The minimum target health percent to cast Rip");
            frm2.AddDropDown("Min Combo Points", "RipMinComboPoints", new List<string>() { "1", "2", "3", "4", "5" }, "Rip", "The minimum combo points you must have to cast Rip");

            frm2.FormHeading = "Rip";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: Cast Rip if the targets health is above 60% [Min Target Health] and your have 3 or more [Min Combo Points] combo points.";

            frm2.Show(this);
        }

        private void ucRip_Load(object sender, System.EventArgs e)
        {

        }

        private void ucNonCombatHealing_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Rejuvenation", "NonCombatRejuvenationHealth", 0, 100, "Rejuvenation", "The minimum health percent at which you will cast Rejuvenation when out of combat");
            frm2.AddProgressBar("Healing Touch", "NonCombatHealingTouchHealth", 0, 100, "Healing Touch", "The minimum health percent at which you will cast Healing Touch when out of combat");

            frm2.FormHeading = "Noncombat Healing";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: When out of combat if your health is below 85% [Rejuvenation] cast Rejuvenation, or 60% [Healing Touch] cast Healing Touch";

            frm2.Show(this);
        }

        private void ucNaturesVigil_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("When", "FeralNaturesVigilWhen", new List<string>() { "... always", "... only on adds", "... only on 3+ adds", "... only on 4+ adds", "... only on 5+ adds" }, "Nature's Vigil", "When to evaluate Nature's Vigil");
            frm2.AddProgressBar("Health", "FeralNaturesVigilHealth", 0, 100, "Nature's Vigil", "The health percent at which Nature's Vigil is considered");

            frm2.FormHeading = "Nature's Vigil";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: Cast Nature's Vigil when you have 3+ adds [When] and your health is below 70% [Health]";

            frm2.Show(this);
        }

        private void ucMoonfire_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {

        }

        private void ucSunfire_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {

        }

        private void ucTravelForm_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Min Distance", "TravelFormMinDistance", new List<string> { "5", "10", "15", "20", "25", "30", "35", "40" }, "", "");

            frm2.FormHeading = "Travel Form";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "The minimum distance of the POI to consider using Travel Form";

            frm2.Show(this);
        }


    }
}
